/*
 * Created by SharpDevelop.
 * User: nebulom
 * Date: 9/2/2009
 * Time: 8:02 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using Buaya.Model;
using System.Collections.Generic;

namespace Buaya.Gui
{
	/// <summary>
	/// Description of DeductionTypeListForm.
	/// </summary>
	public partial class DeductionTypeListForm : BaseForm
	{
		public DeductionTypeListForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			listView1.Resize += delegate { columnHeader1.Width = listView1.Width - 22; };
			refreshToolStripMenuItem.Click += delegate { OnActivated(null); };
			newToolStripMenuItem.Click += delegate { MainForm.Instance.AddChild(new DeductionTypeForm()); };
		}
		
		[System.ComponentModel.EditorBrowsableAttribute()]
		protected override void OnActivated(EventArgs e)
		{
			base.OnActivated(e);
			Model = SessionFacade.FindAllDeductionTypes();
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			listView1.Items.Clear();
			foreach (DeductionType t in Model as IList<DeductionType>) {
				ListViewItem li = listView1.Items.Add(t.Name);
				Util.SwfUtility.AlternateColor(li);
			}
		}
		
		void DeleteToolStripMenuItemClick(object sender, EventArgs e)
		{
			if (listView1.SelectedItems.Count > 0) {
				foreach (ListViewItem li in listView1.SelectedItems) {
					DeductionType t = (Model as IList<DeductionType>)[li.Index];
					SessionFacade.DeleteDeductionType(t);
				}
				OnActivated(null);
			}
		}
		
		void EditToolStripMenuItemClick(object sender, EventArgs e)
		{
			if (listView1.SelectedItems.Count > 0) {
				DeductionType t = (Model as IList<DeductionType>)[listView1.SelectedItems[0].Index];
				MainForm.Instance.AddChild(new DeductionTypeForm(t), "Edit DeductionType");
			}
		}
	}
}
